'use strict';

module.exports = function (grunt) {
  require('load-grunt-tasks')(grunt);
  require('time-grunt')(grunt);

  grunt.initConfig({
    yeoman: {
    },
    clean: {
      dist: {
        files: [{
          dot: true,
          src: [
            '.tmp',
            'dist/cms/*',
            'dist/cms/.git*'
          ]
        }]
      }
    },
    jshint: {
        options: {
            jshintrc: '.jshintrc'
        },
        all: [
            'cms/scripts/{,*/}*.js',
            'ext/components/*/cms/**/*.js'
        ]
    },
    neuter: {
        web: {
            options: {
                filepathTransform: function (filepath) {
                    return 'cms/scripts/' + filepath;
                }
            },
            src: 'cms/scripts/requires.js',
            dest: '.tmp/cms/scripts/scripts-cms.js'
        }
    },
    compass: {
        options: {
            importPath: 'bower_components',
            debugInfo: true,
            sassDir: 'cms/styles',
            cssDir: '.tmp/cms/styles',

            imagesDir: 'cms/img',
            generatedImagesDir: '.tmp/cms/img/generated',
            httpImagesPath: '../../../cms/img',
            httpGeneratedImagesPath: '../img/generated',
            relativeAssets: false
        },
        dist: {
            options: {

            }
        }
    },
    // not used since Uglify task does concat,
    // but still available if needed
    /*concat: {
      dist: {}
    },*/
    useminPrepare: {
      html: 'cms/index.html',
      options: {
        dest: 'dist/cms'
      }
    },

    usemin: {
        html: ['dist/cms/**/*.html'],
        css: ['dist/cms/styles/{,*/}*.css'],
        options: {
            dirs: ['dist/cms']
        }
    },

    imagemin: {
        dist: {
            files: [{
                expand: true,
                cwd: 'cms/img',
                src: ['{,*/}*.{png,jpg,jpeg}'], //, '!**/icons-*/**'],
                dest: 'dist/cms/img'
            }]
        }
    },

    svgmin: {
        dist: {
            files: [{
                expand: true,
                cwd: 'cms/img',
                src: '{,*/}*.svg',
                dest: 'dist/cms/img'
            }]
        }
    },

    cssmin: {
      // By default, your `index.html` <!-- Usemin Block --> will take care of
      // minification. This option is pre-configured if you do not wish to use
      // Usemin blocks.
      // dist: {
      //   files: {
      //     'dist/cms/styles/main.css': [
      //       '.tmp/styles/{,*/}*.css',
      //       'cms/styles/{,*/}*.css'
      //     ]
      //   }
      // }
    },
    htmlmin: {
        dist: {
            options: {
                /*removeCommentsFromCDATA: true,
                 // https://github.com/yeoman/grunt-usemin/issues/44
                 //collapseWhitespace: true,
                 collapseBooleanAttributes: true,
                 removeAttributeQuotes: true,
                 removeRedundantAttributes: true,
                 useShortDoctype: true,
                 removeEmptyAttributes: true,
                 removeOptionalTags: true*/
            },
            files: [{
                expand: true,
                cwd: 'cms',
                src: ['*.html', 'views/**/*.html'],
                dest: 'dist/cms'
            }]
        }
    },
    // Put files not handled in other tasks here
    copy: {
        dist: {
            files: [{
                expand: true,
                dot: true,
                cwd: 'cms',
                dest: 'dist/cms',
                src: [
                    '*.{ico,png,txt}',
                    '.htaccess',
                    'img/{,*/}*.{gif,webp}',
                    'styles/fonts/*'
                ]
            }, {
                expand: true,
                cwd: '.tmp/img',
                dest: 'dist/cms/img',
                src: [
                    'generated/*'
                ]
            },
                {
                    expand: true,
                    dot: true,
                    cwd: 'bower_components/bootstrap/dist',
                    dest: 'dist/cms',
                    src: [
                        'fonts/**'
                    ]
                }]
        },
        styles: {
            expand: true,
            cwd: 'cms/styles',
            dest: '.tmp/cms/styles/',
            src: '{,*/}*.css'
        }
    },
    concurrent: {
      dist: [
          'compass:dist',
          'copy:styles',
          'imagemin',
          'svgmin',
          'htmlmin'
      ]
    },
    ngmin: {
        dist: {
            files: [{
                expand: true,
                cwd: 'dist/cms/scripts',
                src: '*.js',
                dest: 'dist/cms/scripts'
            }]
        }
    },
    uglify: {
        dist: {
            files: {
                'dist/cms/scripts/scripts.js': [
                    'dist/cms/scripts/scripts.js'
                ]
            }
        }
    }
  });

    grunt.registerTask('build', [
        'clean:dist',
        'useminPrepare',
        'concurrent:dist',
        'neuter',
        'concat',
        'copy:dist',
        'ngmin',
        'cssmin',
        'uglify',
        'usemin'
    ]);

    grunt.registerTask('default', [
        'jshint',
        'build'
    ]);
};
